package com.situ.crm.rbac.service.impl;

import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.situ.crm.bean.pojo.User;
import com.situ.crm.bean.vo.UserExcelVO;
import com.situ.crm.rbac.listener.UserExcelListener;
import com.situ.crm.rbac.mapper.UserMapper;
import com.situ.crm.bean.vo.PermissionVO;
import com.situ.crm.bean.vo.RouterVO;
import com.situ.crm.rbac.service.IPermissionService;
import com.situ.crm.rbac.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.situ.crm.rbac.util.RouterUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Gao
 * @since 2023-06-12
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {
    @Autowired
    private IPermissionService permissionService;

    @Override
    public User selectByUsername(String username) {
        return baseMapper.selectOne(new QueryWrapper<User>().eq("name", username));
    }

    @Override
    public Map<String, Object> selectUserPermissionInfo(String username) {
        //根据username查询用户基本信息
        User user = selectByUsername(username);
        //根据userid查询菜单(路由)权限
        List<PermissionVO> permissionVOList =  permissionService.selectRouterListByUserId(user);
        List<RouterVO> routerVOList = RouterUtils.build(permissionVOList);
        //根据userid查询按钮权限
        List<String> btnList = permissionService.selectBtnListByUserId(user);
        Map<String, Object> map = new HashMap<>();
        map.put("name", user.getName());
        map.put("avatar", user.getAvatar());
        map.put("routers", routerVOList);
        map.put("btns", btnList);
        return map;
    }

    @Override
    public void exportData(HttpServletResponse response) {
        try {
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode("user", "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

            //查询数据库，封装数据
            List<User> list = baseMapper.selectList(null);
            List<UserExcelVO> userExcelVOList = new ArrayList<>();
            for (User user : list) {
                UserExcelVO userExcelVO = new UserExcelVO();
                BeanUtils.copyProperties(user, userExcelVO);
                userExcelVOList.add(userExcelVO);
            }
            EasyExcel.write(response.getOutputStream(), UserExcelVO.class).sheet("User信息").doWrite(userExcelVOList);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void importData(MultipartFile file) {
        try {
            EasyExcel.read(file.getInputStream(), UserExcelVO.class, new UserExcelListener(baseMapper)).sheet().doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
